//
//  ViewController.m
//  Alarm3DExperiments4
//
//  Created by 우성 김 on 12. 4. 5..
//  Copyright (c) 2012년 yy01ar@gmail.com. All rights reserved.
//

#import "ViewController.h"
#import "HttpLogger.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    AVAudioSession *session = [AVAudioSession sharedInstance];
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"jungle" withExtension:@"mp3"];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    audioPlayer.delegate = self;
    audioPlayer.volume = 0.0;
    audioPlayer.numberOfLoops = 1;
    [audioPlayer prepareToPlay];
//    [audioPlayer play];
    
    float additionalTime = 15;
//    9 * 60 * 60 - 10 * 60;
    [audioPlayer playAtTime:audioPlayer.deviceCurrentTime + additionalTime];
    
    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:additionalTime];
    NSTimer* timer = [[NSTimer alloc] initWithFireDate:fireDate
                                     interval:3
                                       target:self
                                     selector:@selector(targetMethod:)
                                     userInfo:nil
                                      repeats:YES];
    
    //int timerCount = 1;
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
    
    logger = [[HttpLogger alloc] init];
    
    
    
    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
    if([audioPlayer playAtTime:audioPlayer.deviceCurrentTime + 15.0]) {
        newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    }
     
    
    
//    [audioPlayer playAtTime:audioPlayer.deviceCurrentTime + 15.0];
}


-(void)targetMethod:(NSTimer *)theTimer 
{
    NSLog(@"Timer called");
    if(audioPlayer.volume < 1)
        audioPlayer.volume += 0.05;
    
    NSString *stateString;
    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
    if (state == UIApplicationStateInactive) {
        stateString = @"Inactive State";
    } else if (state == UIApplicationStateBackground) {
        stateString = @"Background State";
    } else {
        stateString = @"Active State";
    }
    NSLog(@"State : %@, Volume : %f\n----------------------------------------", stateString, audioPlayer.volume);
    
    [logger sendSimpleAsyncRequestWithTag:@"timer call" andDescription:[NSString stringWithFormat:@"Timer was called : %@", stateString]];
}

- (IBAction)putRequest:(id)sender {
//    double timeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining];
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://yyar.dothome.co.kr/iphone/"]];
//    NSString *postFormat = [NSString stringWithFormat:@"remain=%f", timeRemaining];
//    NSString *length = [NSString stringWithFormat:@"%d", [postFormat length]] ; 
//    [request addValue:length forHTTPHeaderField:@"Content-Length"];
//    [request setHTTPMethod:@"POST"];
//    [request setHTTPBody:[postFormat dataUsingEncoding:NSUTF8StringEncoding]];
//    
//    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
//    [connection start];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


@end
